home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Binding / Message.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  10.4 KB  |  463 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Binding::Message - Base class for messages
  24.  
  25. =head1 SYNOPSIS
  26.  
  27. Sending a message
  28.  
  29.   my $msg = new Net::DBus::Binding::Message::Signal;
  30.   my $iterator = $msg->iterator;
  31.  
  32.   $iterator->append_byte(132);
  33.   $iterator->append_int32(14241);
  34.  
  35.   $connection->send($msg);
  36.  
  37. =head1 DESCRIPTION
  38.  
  39. Provides a base class for the different kinds of
  40. message that can be sent/received. Instances of
  41. this class are never instantiated directly, rather
  42. one of the four sub-types L<Net::DBus::Binding::Message::Signal>,
  43. L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>,
  44. L<Net::DBus::Binding::Message::Error> should be used.
  45.  
  46. =head1 CONSTANTS
  47.  
  48. The following constants are defined in this module. They are
  49. not exported into the caller's namespace & thus must be referenced
  50. with their fully qualified package names
  51.  
  52. =over 4
  53.  
  54. =item TYPE_ARRAY
  55.  
  56. Constant representing the signature value associated with the
  57. array data type.
  58.  
  59. =item TYPE_BOOLEAN
  60.  
  61. Constant representing the signature value associated with the
  62. boolean data type.
  63.  
  64. =item TYPE_BYTE
  65.  
  66. Constant representing the signature value associated with the
  67. byte data type.
  68.  
  69. =item TYPE_DICT_ENTRY
  70.  
  71. Constant representing the signature value associated with the
  72. dictionary entry data type.
  73.  
  74. =item TYPE_DOUBLE
  75.  
  76. Constant representing the signature value associated with the
  77. IEEE double precision floating point data type.
  78.  
  79. =item TYPE_INT16
  80.  
  81. Constant representing the signature value associated with the
  82. signed 16 bit integer data type.
  83.  
  84. =item TYPE_INT32
  85.  
  86. Constant representing the signature value associated with the
  87. signed 32 bit integer data type.
  88.  
  89. =item TYPE_INT64
  90.  
  91. Constant representing the signature value associated with the
  92. signed 64 bit integer data type.
  93.  
  94. =item TYPE_OBJECT_PATH
  95.  
  96. Constant representing the signature value associated with the
  97. object path data type.
  98.  
  99. =item TYPE_STRING
  100.  
  101. Constant representing the signature value associated with the
  102. UTF-8 string data type.
  103.  
  104. =item TYPE_SIGNATURE
  105.  
  106. Constant representing the signature value associated with the
  107. signature data type.
  108.  
  109. =item TYPE_STRUCT
  110.  
  111. Constant representing the signature value associated with the
  112. struct data type.
  113.  
  114. =item TYPE_UINT16
  115.  
  116. Constant representing the signature value associated with the
  117. unsigned 16 bit integer data type.
  118.  
  119. =item TYPE_UINT32
  120.  
  121. Constant representing the signature value associated with the
  122. unsigned 32 bit integer data type.
  123.  
  124. =item TYPE_UINT64
  125.  
  126. Constant representing the signature value associated with the
  127. unsigned 64 bit integer data type.
  128.  
  129. =item TYPE_VARIANT
  130.  
  131. Constant representing the signature value associated with the
  132. variant data type.
  133.  
  134. =back
  135.  
  136. =head1 METHODS
  137.  
  138. =over 4
  139.  
  140. =cut
  141.  
  142. package Net::DBus::Binding::Message;
  143.  
  144. use 5.006;
  145. use strict;
  146. use warnings;
  147.  
  148. use Net::DBus::Binding::Iterator;
  149. use Net::DBus::Binding::Message::Signal;
  150. use Net::DBus::Binding::Message::MethodCall;
  151. use Net::DBus::Binding::Message::MethodReturn;
  152. use Net::DBus::Binding::Message::Error;
  153.  
  154. =item my $msg = Net::DBus::Binding::Message->new(message => $rawmessage);
  155.  
  156. Creates a new message object, initializing it with the underlying C
  157. message object given by the C<message> object. This constructor is
  158. intended for internal use only, instead refer to one of the four
  159. sub-types for this class for specific message types
  160.  
  161. =cut
  162.  
  163. sub new {
  164.     my $proto = shift;
  165.     my $class = ref($proto) || $proto;
  166.     my %params = @_;
  167.     my $self = {};
  168.  
  169.     $self->{message} = exists $params{message} ? $params{message} : 
  170.     (Net::DBus::Binding::Message::_create(exists $params{type} ? $params{type} : die "type parameter is required"));
  171.  
  172.     bless $self, $class;
  173.     
  174.     if ($class eq "Net::DBus::Binding::Message") {
  175.     $self->_specialize;
  176.     }
  177.  
  178.     return $self;
  179. }
  180.  
  181. sub _specialize {
  182.     my $self = shift;
  183.     
  184.     my $type = $self->get_type;
  185.     if ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_CALL) {
  186.     bless $self, "Net::DBus::Binding::Message::MethodCall";
  187.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_METHOD_RETURN) {
  188.     bless $self, "Net::DBus::Binding::Message::MethodReturn";
  189.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_ERROR) {
  190.     bless $self, "Net::DBus::Binding::Message::Error";
  191.     } elsif ($type == &Net::DBus::Binding::Message::MESSAGE_TYPE_SIGNAL) {
  192.     bless $self, "Net::DBus::Binding::Message::Signal";
  193.     } else {
  194.     warn "Unknown message type $type\n";
  195.     }
  196. }
  197.  
  198. =item my $type = $msg->get_type
  199.  
  200. Retrieves the type code for this message. The returned value corresponds
  201. to one of the four C<Net::DBus::Binding::Message::MESSAGE_TYPE_*> constants.
  202.  
  203. =cut
  204.  
  205. sub get_type {
  206.     my $self = shift;
  207.  
  208.     return $self->{message}->dbus_message_get_type;
  209. }
  210.  
  211. =item my $interface = $msg->get_interface
  212.  
  213. Retrieves the name of the interface targetted by this message, possibly
  214. an empty string if there is no applicable interface for this message.
  215.  
  216. =cut
  217.  
  218. sub get_interface {
  219.     my $self = shift;
  220.     
  221.     return $self->{message}->dbus_message_get_interface;
  222. }
  223.  
  224. =item my $path = $msg->get_path
  225.  
  226. Retrieves the object path associated with the message, possibly an
  227. empty string if there is no applicable object for this message.
  228.  
  229. =cut
  230.  
  231. sub get_path {
  232.     my $self = shift;
  233.     
  234.     return $self->{message}->dbus_message_get_path;
  235. }
  236.  
  237. =item my $name = $msg->get_destination
  238.  
  239. Retrieves the uniqe or well-known bus name for client intended to be
  240. the recipient of the message. Possibly returns an empty string if
  241. the message is being broadcast to all clients.
  242.  
  243. =cut
  244.  
  245. sub get_destination {
  246.     my $self = shift;
  247.     
  248.     return $self->{message}->dbus_message_get_destination;
  249. }
  250.  
  251. =item my $name = $msg->get_sender
  252.  
  253. Retireves the unique name of the client sending the message
  254.  
  255. =cut
  256.  
  257. sub get_sender {
  258.     my $self = shift;
  259.     
  260.     return $self->{message}->dbus_message_get_sender;
  261. }
  262.  
  263. =item my $serial = $msg->get_serial
  264.  
  265. Retrieves the unique serial number of this message. The number
  266. is guarenteed unique for as long as the connection over which
  267. the message was sent remains open. May return zero, if the message
  268. is yet to be sent.
  269.  
  270. =cut
  271.  
  272. sub get_serial {
  273.     my $self = shift;
  274.     
  275.     return $self->{message}->dbus_message_get_serial;
  276. }
  277.  
  278. =item my $name = $msg->get_member
  279.  
  280. For method calls, retrieves the name of the method to be invoked,
  281. while for signals, retrieves the name of the signal.
  282.  
  283. =cut
  284.  
  285. sub get_member {
  286.     my $self = shift;
  287.     
  288.     return $self->{message}->dbus_message_get_member;
  289. }
  290.  
  291. =item my $sig = $msg->get_signature
  292.  
  293. Retrieves a string representing the type signature of the values
  294. packed into the body of the message.
  295.  
  296. =cut
  297.  
  298. sub get_signature {
  299.     my $self = shift;
  300.     
  301.     return $self->{message}->dbus_message_get_signature;
  302. }
  303.  
  304. =item $msg->set_sender($name)
  305.  
  306. Set the name of the client sending the message. The name must
  307. be the unique name of the client.
  308.  
  309. =cut
  310.  
  311. sub set_sender {
  312.     my $self = shift;
  313.     $self->{message}->dbus_message_set_sender(@_);
  314. }
  315.  
  316. =item $msg->set_destination($name)
  317.  
  318. Set the name of the intended recipient of the message. This is
  319. typically used for signals to switch them from broadcast to
  320. unicast.
  321.  
  322. =cut
  323.  
  324. sub set_destination {
  325.     my $self = shift;
  326.     $self->{message}->dbus_message_set_destination(@_);
  327. }
  328.  
  329. =item my $iterator = $msg->iterator;
  330.  
  331. Retrieves an iterator which can be used for reading or
  332. writing fields of the message. The returned object is
  333. an instance of the C<Net::DBus::Binding::Iterator> class.
  334.  
  335. =cut
  336.  
  337. sub iterator {
  338.     my $self = shift;
  339.     my $append = @_ ? shift : 0;
  340.     
  341.     if ($append) {
  342.     return Net::DBus::Binding::Message::_iterator_append($self->{message});
  343.     } else {
  344.     return Net::DBus::Binding::Message::_iterator($self->{message});
  345.     }
  346. }
  347.  
  348. =item $boolean = $msg->get_no_reply()
  349.  
  350. Gets the flag indicating whether the message is expecting
  351. a reply to be sent. 
  352.  
  353. =cut
  354.  
  355. sub get_no_reply {
  356.     my $self = shift;
  357.     
  358.     return $self->{message}->dbus_message_get_no_reply;
  359. }
  360.  
  361. =item $msg->set_no_reply($boolean)
  362.  
  363. Toggles the flag indicating whether the message is expecting
  364. a reply to be sent. All method call messages expect a reply
  365. by default. By toggling this flag the communication latency
  366. is reduced by removing the need for the client to wait
  367.  
  368. =cut
  369.  
  370.  
  371. sub set_no_reply {
  372.     my $self = shift;
  373.     my $flag = shift;
  374.     
  375.     $self->{message}->dbus_message_set_no_reply($flag);
  376. }
  377.  
  378. =item my @values = $msg->get_args_list
  379.  
  380. De-marshall all the values in the body of the message, using the 
  381. message signature to identify data types. The values are returned
  382. as a list.
  383.  
  384. =cut
  385.  
  386. sub get_args_list {
  387.     my $self = shift;
  388.     
  389.     my @ret;    
  390.     my $iter = $self->iterator;
  391.     if ($iter->get_arg_type() != &Net::DBus::Binding::Message::TYPE_INVALID) {
  392.     do {
  393.         push @ret, $iter->get();
  394.     } while ($iter->next);
  395.     }
  396.  
  397.     return @ret;
  398. }
  399.  
  400. =item $msg->append_args_list(@values)
  401.  
  402. Append a set of values to the body of the message. Values will
  403. be encoded as either a string, list or dictionary as appropriate
  404. to their Perl data type. For more specific data typing needs,
  405. the L<Net::DBus::Binding::Iterator> object should be used instead.
  406.  
  407. =cut
  408.  
  409. sub append_args_list {
  410.     my $self = shift;
  411.     my @args = @_;
  412.     
  413.     my $iter = $self->iterator(1);
  414.     foreach my $arg (@args) {
  415.     $iter->append($arg);
  416.     }
  417. }
  418.  
  419. # To keep autoloader quiet
  420. sub DESTROY {
  421. }
  422.  
  423. sub AUTOLOAD {
  424.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  425.     # XS function.
  426.  
  427.     my $constname;
  428.     our $AUTOLOAD;
  429.     ($constname = $AUTOLOAD) =~ s/.*:://;
  430.  
  431.     die "&Net::DBus::Binding::Message::constant not defined" if $constname eq '_constant';
  432.  
  433.     if (!exists $Net::DBus::Binding::Message::_constants{$constname}) {
  434.         die "no such constant \$Net::DBus::Binding::Message::$constname";
  435.     }
  436.  
  437.     {
  438.     no strict 'refs';
  439.     *$AUTOLOAD = sub { $Net::DBus::Binding::Message::_constants{$constname} };
  440.     }
  441.     goto &$AUTOLOAD;
  442. }
  443.  
  444. 1;
  445.  
  446. =pod
  447.  
  448. =back
  449.  
  450. =head1 SEE ALSO
  451.  
  452. L<Net::DBus::Binding::Server>, L<Net::DBus::Binding::Connection>, L<Net::DBus::Binding::Message::Signal>, L<Net::DBus::Binding::Message::MethodCall>, L<Net::DBus::Binding::Message::MethodReturn>, L<Net::DBus::Binding::Message::Error>
  453.  
  454. =head1 AUTHOR
  455.  
  456. Daniel Berrange E<lt>dan@berrange.comE<gt>
  457.  
  458. =head1 COPYRIGHT
  459.  
  460. Copyright 2004 by Daniel Berrange
  461.  
  462. =cut
  463.